package com.etc.traval.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.etc.traval.biz.DiaryBiz;
import com.etc.traval.biz.DiarycomBiz;
import com.etc.traval.biz.PlaceBiz;
import com.etc.traval.bizimpl.DiaryBizImpl;
import com.etc.traval.bizimpl.DiarycomBizImpl;
import com.etc.traval.bizimpl.PlaceBizImpl;
import com.etc.traval.dao.PlaceDao;
import com.etc.traval.daoimpl.PlaceDaoImpl;
import com.etc.traval.util.FileUp;
import com.etc.traval.util.MyUtil;
import com.etc.traval.util.RenamePolicyCos;
import com.etc.travel.entity.Diary;
import com.etc.travel.entity.Place;
import com.etc.travel.entity.User;
import com.google.gson.Gson;
import com.oreilly.servlet.MultipartRequest;

public class DiaryAddServlet extends HttpServlet {

	/**
		 * Constructor of the object.
		 */
	public DiaryAddServlet() {
		super();
	}

	/**
		 * Destruction of the servlet. <br>
		 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);
	}

	/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		DiaryBiz diaryBiz = new DiaryBizImpl();
		PlaceBiz placeBiz = new PlaceBizImpl();
			String savepath = request.getRealPath("/diaryimage");
			int maxsize = 10 * 1024 * 1024;
			//构造一个MultipartRequest对象，依次传入内置对象request,保存路径，上传的最大长度，编码格式
			MultipartRequest req = new MultipartRequest(request, savepath, maxsize, "utf-8", new RenamePolicyCos());
			String diarytitle = req.getParameter("input");
			String diarycontent = req.getParameter("diarycontent");
			int placeid =Integer.valueOf(req.getParameter("selection"));
			String files=null;
			try {
				files = FileUp.uploadfiles(req);
			} catch (Exception e) {
				e.printStackTrace();
			}
			Diary diary = new Diary();
			diary.setDiary_content(diarycontent);
			HttpSession session =request.getSession();
			User user = (User) session.getAttribute("user");
			diary.setDiary_user(user);
			diary.setDiary_logo(files);
			diary.setDiary_place(placeBiz.queryPlaceById(placeid));
			diary.setDiary_title(diarytitle);
			MyUtil myUtil = new MyUtil();
			Timestamp date = myUtil.getWebsiteDatetime();
			response.setContentType("text/html;charset=utf-8");
			if(date==null){
				PrintWriter out = response.getWriter();
				out.print("<script>alert('网络不稳定');history.go(-1);</script>");
			}
			diary.setDiary_time(date);
			
			if(diaryBiz.addDiary(diary)){
				PrintWriter out = response.getWriter();
				out.print("<script>alert('发表成功');location.href='DiaryServlet?flag=alldiary';</script>");
				request.getRequestDispatcher("DiaryServlet?flag=alldiary").forward(request, response);
			}else {
				PrintWriter out = response.getWriter();
				out.print("<script>alert('发表失败');history.go(-1);</script>");
			}		
	}

	/**
		 * Initialization of the servlet. <br>
		 *
		 * @throws ServletException if an error occurs
		 */
	public void init() throws ServletException {
		// Put your code here
	}

}
